package com.water.Realm;


import cn.hutool.http.HttpRequest;
import com.water.entity.SyEmp;
import com.water.entity.SyEmpmenupower;
import com.water.service.SyEmpService;
import com.water.service.SyEmpmenupowerService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class myRealm extends AuthorizingRealm {

    @Autowired
    private SyEmpService syEmpService;

    @Autowired
    private SyEmpmenupowerService syEmpmenupowerService;


    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
       UsernamePasswordToken usernamePasswordToken= (UsernamePasswordToken)  authenticationToken;
       System.out.println("doGetAuthenticationInfo");
        String empno = usernamePasswordToken.getUsername();
         SyEmp syEmp1=new SyEmp();
        syEmp1.setEmpno(empno);
        SyEmp syEmp = syEmpService.querySy(syEmp1);
        String realName = this.getName();
        SimpleAuthenticationInfo info=new SimpleAuthenticationInfo(syEmp.getEmpno(),syEmp.getPwd(),realName);
        return info;
    }


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String princaipal = (String) principalCollection.getPrimaryPrincipal();
        Set<String> roles=new HashSet<>();
        System.out.println("===============================================================================================================");

        System.out.println(princaipal);
        System.out.println("===============================================================================================================");
        SyEmp syEmp1=new SyEmp();
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
      SyEmp syEmp= (SyEmp) session.getAttribute("user");
     String id= syEmp.getId().toString();
 List<SyEmpmenupower> syEmpmenupowerList=   syEmpmenupowerService.findQuanList(id);
                //        //给一个初始权限
        for (SyEmpmenupower syEmpmenupower : syEmpmenupowerList) {
            System.out.println(syEmpmenupower.getMenuid());
            Integer menuid=syEmpmenupower.getMenuid();
            roles.add(menuid.toString());
        }

        roles.add("user");
//        admin admin = adminService.queryOneAdmin(princaipal);
//        int authoid = admin.getAuthority().getAuthoid();
//        //给权限
//        if(authoid>=2){
            roles.add("admin");
//        }
        System.out.println(roles+"所有的权限");
        SimpleAuthorizationInfo info=new SimpleAuthorizationInfo(roles);

        return info;
    }


}
